package com.dhbw.ui.ctrl;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

import com.dhbw.ui.model.CalculationBean;

@ManagedBean(name="calcCtrl")
@RequestScoped
public class CaculationUIController {

	@ManagedProperty(value = "#{calculationBean}")
	private CalculationBean calcBean;
	
	public String loadList(){
		//TODO aus DB lesen
		calcBean.getAllPaymentTypes().add(new SelectItem("KK", "Kreditkarte"));
		calcBean.getAllPaymentTypes().add(new SelectItem("NA", "Naturalien"));
		calcBean.getAllPaymentTypes().add(new SelectItem("BE", "Bankeinzug"));
		
		return null;
		
	}

	public String calculate() {
		
		calcBean.setResult(calcBean.getNo1() + calcBean.getNo2());

		return "result";
	}
	
	public void vadidateValue(FacesContext jfsCtx,
			UIComponent cmp,
			Object chechValue) throws ValidatorException{
		int value= (Integer) chechValue;
		
		if(value==13){
			FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Zahl ist nicht gut" , "Diese Zahl ist keine passende Zahl.");
			throw new ValidatorException(m);
		}
		
	}

	public void setCalcBean(CalculationBean calcBean) {
		this.calcBean = calcBean;
	}

}
